# -*- coding:UTF-8 -*-

"""
服务器主程序
"""

import tornado.web
import tornado.ioloop
import tornado.gen
import os
import xls
import QrOpt

settings = {
    "static_path":os.path.join(os.path.dirname(__file__), "static"),
    "cookie_secret": "61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
    "login_url": "/",

}  #设置服务端静态文件地址，cookie_secret，默认地址等信息

class MainHandler(tornado.web.RequestHandler):#登录用句柄，为登录提供后台服务
    @tornado.gen.engine
    @tornado.web.asynchronous
    def get(self):#在当前句柄中，得到ｇｅｔ请求后的操作，后面的get函数同样效果
        self.render("static/html/index.html")#操作内容，显示参数路径中的网页

class DataHandler(tornado.web.RequestHandler):
    @tornado.gen.engine
    @tornado.web.asynchronous
    def get(self):
        xlsf = xls.xlsfile()
        xls_json = xlsf.get_rows_json()
        self.write(xls_json)
        self.finish()

class QrHandler(tornado.web.RequestHandler):
    @tornado.gen.engine
    @tornado.web.asynchronous
    def post(self):
        contact = self.get_argument("contact")
        msg = self.get_argument("msg")
        Qr = QrOpt.Qrobj()
        Qr.update(contact,msg)
        img = Qr.get_img()




application = tornado.web.Application([(r"/",MainHandler),
                                       (r"/data",DataHandler),
                                       ],**settings)#网站后台设置

if __name__ == '__main__':
    application.listen(8081)#开启端口监听
    tornado.ioloop.IOLoop.instance().start()#开启监听循环
